package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysIpBo;
import com.ruoyi.system.domain.vo.SysIpVo;
import com.ruoyi.system.domain.SysIp;
import com.ruoyi.system.mapper.SysIpMapper;
import com.ruoyi.system.service.ISysIpService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * ip设置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-23
 */
@RequiredArgsConstructor
@Service
public class SysIpServiceImpl implements ISysIpService {

    private final SysIpMapper baseMapper;

    /**
     * 查询ip设置
     */
    @Override
    public SysIpVo queryById(Long infoId){
        return baseMapper.selectVoById(infoId);
    }

    /**
     * 查询ip设置列表
     */
    @Override
    public TableDataInfo<SysIpVo> queryPageList(SysIpBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysIp> lqw = buildQueryWrapper(bo);
        Page<SysIpVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ip设置列表
     */
    @Override
    public List<SysIpVo> queryList(SysIpBo bo) {
        LambdaQueryWrapper<SysIp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysIp> buildQueryWrapper(SysIpBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysIp> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), SysIp::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getIpaddr()), SysIp::getIpaddr, bo.getIpaddr());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysIp::getStatus, bo.getStatus());
        lqw.eq(bo.getLoginTime() != null, SysIp::getLoginTime, bo.getLoginTime());
        return lqw;
    }

    /**
     * 新增ip设置
     */
    @Override
    public Boolean insertByBo(SysIpBo bo) {
        SysIp add = BeanUtil.toBean(bo, SysIp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInfoId(add.getInfoId());
        }
        return flag;
    }

    /**
     * 修改ip设置
     */
    @Override
    public Boolean updateByBo(SysIpBo bo) {
        SysIp update = BeanUtil.toBean(bo, SysIp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean changeIp(SysIpBo bo) {
        SysIp sysIp = new SysIp();
        sysIp.setInfoId(bo.getInfoId());
        sysIp.setStatus(bo.getStatus());
        int i = baseMapper.updateById(sysIp);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysIp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ip设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
